﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Micronet.OpenWeb.Com.Controls;
using Micronet.OpenWeb.Entity;
using Micronet.OpenWeb.Utils;
using Telerik.Web.UI;
using Micronet.OpenWeb.Com.BLL;

namespace Micronet.OpenWeb.Com.ControlLib
{
	public partial class PromotionEdit : System.Web.UI.UserControl
	{
	    public DateTime dateBegin;
        public DateTime DateBegin
        {
            get { return ViewState["dateBegin"] != null ? (DateTime)ViewState["dateBegin"] : DateTime.Now; }
            set { ViewState["dateBegin"] = value; }
        }

	    public DateTime dateEnd;
        public DateTime DateEnd
        {
            get { return ViewState["dateEnd"] != null ? (DateTime)ViewState["dateEnd"] : DateTime.Now; }
            set { ViewState["dateEnd"] = value; }
        }

		protected void Page_Load(object sender, EventArgs e)
		{
            Init();
		}

        //Khoi tao
        public void Init()
        {
            MonDayCheckBox.Enabled = false;
            TuesDayCheckBox.Enabled = false;
            WednesDayDayCheckBox.Enabled = false;
            ThursDayCheckBox.Enabled = false;
            FriDayCheckBox.Enabled = false;
            SaturDayCheckBox.Enabled = false;
            SunDayCheckBox.Enabled = false;
            if (!IsPostBack)
            {
                ProductTabStrip.Visible = false;
                ProductMultiPage.Visible = false;
                CustomerTabStrip.Visible = false;
                CustomerMultiPage.Visible = false;
                OrderPromotionAjaxPanel.Visible = false;
                ProductPromotionLabel.Visible = false;
                ChooseGiftLabel.Visible = false;
                GiftPromotionCombobox.Visible = false;
                AddGiftHyperLink.Visible = false;
            }
        }

        protected void DateBeginDateTimePicker_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {
            if (DateBeginDateTimePicker.SelectedDate != null) DateBegin = DateBeginDateTimePicker.SelectedDate.Value;
        }

        protected void DateEndDateTimePicker_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {
            if (DateEndDateTimePicker.SelectedDate != null) DateEnd = DateEndDateTimePicker.SelectedDate.Value;
            TimeSpan diff = DateEnd - DateBegin;
            int days = diff.Days;
            for (int i = 0; i <= days && i<=7; i++)
            {
                var testDate = DateBegin.AddDays(i);
                switch (testDate.DayOfWeek)
                {
                    case DayOfWeek.Monday:
                        MonDayCheckBox.Enabled = true;
                        break;
                    case DayOfWeek.Tuesday:
                        TuesDayCheckBox.Enabled = true;
                        break;
                    case DayOfWeek.Wednesday:
                        WednesDayDayCheckBox.Enabled = true;
                        break;
                    case DayOfWeek.Thursday:
                        ThursDayCheckBox.Enabled = true;
                        break;
                    case DayOfWeek.Friday:
                        FriDayCheckBox.Enabled = true;
                        break;
                    case DayOfWeek.Saturday:
                        SaturDayCheckBox.Enabled = true;
                        break;
                    case DayOfWeek.Sunday:
                        SunDayCheckBox.Enabled = true;
                        break;
                }
            }
        }

        protected void PromotionPriceTypeCombobox_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            PromotionPriceNumbericTextBox.Type = NumericType.Currency;
            PromotionPriceNumbericTextBox.MaxValue = 70368744177664;
            PromotionPriceNumbericTextBox.MinValue = 0;
            PromotionPriceNumbericTextBox.Culture =
                System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag(e.Value);
            HiddenCurrencyIDPromotionCombobox.SelectedIndex = PromotionPriceTypeCombobox.SelectedIndex;
        }

        protected void ProductPriceTypeCombobox_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            ProductPriceNumbericTextBox.Type = NumericType.Currency;
            ProductPriceNumbericTextBox.MaxValue = 70368744177664;
            ProductPriceNumbericTextBox.MinValue = 0;
            ProductPriceNumbericTextBox.Culture =
                System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag(e.Value);
            HiddenCurrencyIDProductCombobox.SelectedIndex = ProductPriceTypeCombobox.SelectedIndex;
        }

        protected void TotalAmountOrderTypeCombobox_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            TotalAmountOrderNumbericTextBox.Type = NumericType.Currency;
            TotalAmountOrderNumbericTextBox.MaxValue = 70368744177664;
            TotalAmountOrderNumbericTextBox.MinValue = 0;
            TotalAmountOrderNumbericTextBox.Culture =
                System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag(e.Value);
            HiddenCurrencyIDOrderCombobox.SelectedIndex = TotalAmountOrderTypeCombobox.SelectedIndex;
        }

        protected void TotalAmountCustomerTypeCombobox_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            TotalAmountCustomerNumbericTextBox.Type = NumericType.Currency;
            TotalAmountCustomerNumbericTextBox.MaxValue = 70368744177664;
            TotalAmountCustomerNumbericTextBox.MinValue = 0;
            TotalAmountCustomerNumbericTextBox.Culture =
                System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag(e.Value);
            HiddenCurrencyIDCustomerCombobox.SelectedIndex = TotalAmountCustomerTypeCombobox.SelectedIndex;
        }

        protected void CreatePromotionCampaignButton_Click(object sender, EventArgs e)
        {
            #region promotionObject
            var promotionObject = new OwPromotionsEntity();
            //PromotionCampaignID:
            promotionObject.PromotionCampaignID = "Id".OwQsGetValue().OwToInt64TryParseReturn0();
            //promotionObject.CurrencyID = chua lam
            promotionObject.CurrencyID = HiddenCurrencyIDPromotionCombobox.SelectedValue.OwToInt64TryParseReturn0();
            //Ispercent
            promotionObject.Ispercent = ProductPriceRadioButtonList.SelectedValue.OwToBoolean();
            //Price Promotion
            promotionObject.PricePromotion = PromotionPriceNumbericTextBox.Text.OwToDouble();
            //card
            promotionObject.IsCard = IsCardCheckBox.Checked.OwToBoolean();
            //gift
            promotionObject.IsGift = IsGiftCheckBox.Checked.OwToBoolean();
            promotionObject.GiftID = GiftPromotionCombobox.SelectedValue.OwToInt64TryParseReturn0();
            // Apply for all: product & customer
            promotionObject.ApplyForAllProduct = ProductPromotionRadioButtonList.SelectedValue.OwToBoolean();
            promotionObject.ApplyForAllCustomer = CustomerPromotionRadioButtonList.SelectedValue.OwToBoolean();
            // Date 
            promotionObject.DateTimeBegin = DateBeginDateTimePicker.SelectedDate;
            promotionObject.DateTimeEnd = DateEndDateTimePicker.SelectedDate;
            // Day
            promotionObject.DayOfWeek = getDayList(MonDayCheckBox, TuesDayCheckBox, WednesDayDayCheckBox,
                                                   ThursDayCheckBox, FriDayCheckBox, SaturDayCheckBox, SunDayCheckBox);
            // Time
            promotionObject.TimeOfDayBegin = TimeBeginRadMaskedTextBox.Text;
            promotionObject.TimeOfDayEnd = TimeEndRadMaskedTextBox.Text;
            /* 
             * IsSelectObjectToPromotion
             1 - ProductInPromotion
             2 - ProductRequires
             3 - OrderRequires
             */
            switch (ProductTabStrip.SelectedIndex)
            {
                case 0:
                    promotionObject.IsSelectObjectToPromotion = 1;
                    break;
                case 1:
                    promotionObject.IsSelectObjectToPromotion = 2;
                    break;
                case 2:
                    promotionObject.IsSelectObjectToPromotion = 3;
                    break;
            }
            /* 
             * IsSelectObjectReceivePromotion
             1 - CustomerInPromotion
             2 - CustomerRequires
             */
            switch (CustomerTabStrip.SelectedIndex)
            {
                case 0:
                    promotionObject.IsSelectObjectReceivePromotion = 1;
                    break;
                case 1:
                    promotionObject.IsSelectObjectReceivePromotion = 2;
                    break;
            }
            // IsPricePromotion???
            promotionObject.IsPricePromotion = true;
            
            if(OwPromotionsBLL.OwSave(promotionObject)>0)
            {
                Response.Write("Oh yeah");
            }

            #endregion

        }

	    protected string getDayList (params OwCheckBox[] cBoxs)
	    {
	        return cBoxs.Where(owCheckBox => owCheckBox.Checked.OwToBoolean()).Aggregate("", (current, owCheckBox) => current + (owCheckBox.Text.Trim() + ";"));
	    }

	    protected void ProductPromotionRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (ProductPromotionRadioButtonList.SelectedIndex)
            {
                case 0:
                    ProductTabStrip.Visible = false;
                    ProductMultiPage.Visible = false;
                    OrderPromotionAjaxPanel.Visible = false;
                    ProductPromotionLabel.Visible = false;
                    break;
                case 1:
                    ProductTabStrip.Visible = true;
                    ProductMultiPage.Visible = true;
                    OrderPromotionAjaxPanel.Visible = true;
                    ProductPromotionLabel.Visible = true;
                    break;
            }
        }

        protected void CustomerPromotionRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (CustomerPromotionRadioButtonList.SelectedIndex)
            {
                case 0:
                    CustomerTabStrip.Visible = false;
                    CustomerMultiPage.Visible = false;
                    break;
                case 1:
                    CustomerTabStrip.Visible = true;
                    CustomerMultiPage.Visible = true;
                    break;
            }
        }

        protected void ProductPriceRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (ProductPriceRadioButtonList.SelectedValue)
            {
                case "0":
                    PromotionPriceTypeCombobox.Visible = true;
                    break;         
                case "1":
                    PromotionPriceTypeCombobox.Visible = false;
                    PromotionPriceNumbericTextBox.Type = NumericType.Percent;
                    PromotionPriceNumbericTextBox.MaxValue = 100;
                    PromotionPriceNumbericTextBox.MinValue = 0;
                    break;
            }
        }

        protected void IsGiftCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            ChooseGiftLabel.Visible = (bool) IsGiftCheckBox.Checked;
            GiftPromotionCombobox.Visible = (bool) IsGiftCheckBox.Checked;
            AddGiftHyperLink.Visible = (bool)IsGiftCheckBox.Checked;
        }
	}
}